package com.study.concurrent.completablefuture;

import java.util.concurrent.*;

/**
 * The type CompletableFuture test.
 */
public class CompletableFutureTest {

    private static ExecutorService executor= Executors.newFixedThreadPool(3);

    /**
     * The entry point of application.
     *
     * @param args the input arguments
     * @throws ExecutionException   the execution exception
     * @throws InterruptedException the interrupted exception
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture completableFuture=CompletableFuture.runAsync(()->{
            System.out.println("Runnable 执行");
        });
        //如果没有返回值则get返回null
        System.out.println("completableFuture.get() = " + completableFuture.get());

        completableFuture=CompletableFuture.runAsync(()->{
            System.out.println("Runnable + Executor 执行");
        },executor);
        //如果没有返回值则get返回null
        System.out.println("completableFuture.get() = " + completableFuture.get());


        //有返回值的 supplyAsync
        completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("Callable 执行");
            return 1;
        });
        //如果没有返回值则get返回null
        System.out.println("completableFuture.get() = " + completableFuture.get());

        completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("Callable + Executor 执行");
            return 1;
        },executor);
        //如果没有返回值则get返回null
        System.out.println("completableFuture.get() = " + completableFuture.get());


        executor.shutdown();
    }
}
